package com.zhaogang.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhaogang.entity.SaleAccount;
import com.zhaogang.entity.SaleDetails;
import com.zhaogang.service.SaleInfoService;

/**
* @author
* @data
* @version
* @parameter
* @return
*/
@Controller
@RequestMapping("/saleInfo")
public class SaleCenterController {
	@Autowired
	private SaleInfoService saleInfoService;
	
	@ResponseBody
	@RequestMapping("/check")
	public int checkSaleAccout(String saleName) throws Exception{
		int flag=saleInfoService.validateAccount(saleName);
		return flag;
	}
	
	@ResponseBody
	@RequestMapping("/getSaleInfo")
	public ModelAndView getsaleInfo(HttpSession session){
		SaleAccount sale=(SaleAccount) session.getAttribute("saleToken");
		String account=sale.getSaleAccount();
		Map<String,Object> map=new HashMap<String, Object>();
		//account="aa127677";
		try {
			map= saleInfoService.selectSaleInfo(account);
			System.out.println(map);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return new ModelAndView("goods/saleCenter",map);
		
	}	

	@ResponseBody
	@RequestMapping("/modifyPW")
	public int modifyPSW(String newpsw,String oldpsw,HttpSession session){
		List<String> list=new ArrayList<String>();
		SaleAccount saleAccount=(SaleAccount) session.getAttribute("saleToken");
		if(saleAccount==null)
		{
			return 0;
		}
		String account=saleAccount.getSaleAccount();
		list.add(newpsw);
		list.add(oldpsw);
		list.add(account);
		int i=4;
		try {
			i = saleInfoService.updateAccountPW(list);
			System.out.println(i);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	@RequestMapping("/modifyInfo")
	public void modifyInfo(HttpServletRequest req,HttpServletResponse res){
		SaleAccount saleAccount=new SaleAccount();
		SaleDetails saleDetails=new SaleDetails();
		List <Object> list=new ArrayList<Object>();
		//商户基本信息
		int accountId=Integer.parseInt(req.getParameter("accountId"));
		String account=req.getParameter("saleAccount");
		String nickname=req.getParameter("nickname");
		saleAccount.setId(accountId);
		saleAccount.setSaleAccount(account);
		saleAccount.setNickname(nickname);
		list.add(0, saleAccount);
		//商户详细信息
		String realname=req.getParameter("realname");
		String gender=req.getParameter("gender");
		String saleAddress=req.getParameter("saleAddress");
		String phone=req.getParameter("phone");
		saleDetails.setSaleAccountId(accountId);
		saleDetails.setRealname(realname);
		saleDetails.setGender(gender);
		saleDetails.setAddress(saleAddress);
		saleDetails.setPhone(phone);
		list.add(1, saleDetails);
		try {
			saleInfoService.updateSaleInfo(list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
  
	
	


}
